//
//  SFPAddMaternityNotesController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit
import ProgressHUD
import BRPickerView
class SFPAddMaternityNotesController: SFPBaseViewController {
    
    @IBOutlet var dateTF: UITextField!
    @IBOutlet var timesLabel: UILabel!
    @IBOutlet var temLabel: UILabel!
    @IBOutlet var feelingTV: UITextView!
    var temp = ""
    var times = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        self.feelingTV.placeholder = "Please enter feeling today"
        self.feelingTV.placeholderColor = .lightGray
        // Do any additional setup after loading the view.
    }
    
    @IBAction func gotoBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func gotoSave(_ sender: Any) {
        self.view.endEditing(true)
        if self.temp.count < 1 {
            showTips("Please select body temperature")
            return
        }
        if self.times.count < 1 {
            showTips("Please select number of fetal movements")
            return
        }
        if self.dateTF.text!.isEmpty{
            showTips("Please select record date")
            return
        }
        if self.feelingTV.text!.isEmpty {
            showTips("Please enter feeling today")
            return
        }
        let model = SFPMaternityNotesModel()
        model.temperature = self.temp
        model.quicken = self.timesLabel.text!
        model.date = self.dateTF.text!
        model.feel = self.feelingTV.text!
        
        model.isDefault = false
        model.saveToDB()
        let helper = SFPMaternityNotesModel.getUsingLKDBHelper()
        helper.delete(with: SFPMaternityNotesModel.self, where: "isDefault = true")
        ProgressHUD.showSuccess("Add note succeed")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    @IBAction func gotoSelectTemperature(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRStringPickerView(pickerMode: .componentMulti)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.dataSourceArr = self.getTemperature()
        pickerView.resultModelArrayBlock = { results in
            self.temLabel.text = String(format: "%@.%@℃", (results?.first?.value)!,(results?.last?.value)!)
            self.temp = String(format: "%@.%@", (results?.first?.value)!,(results?.last?.value)!)
        }
        pickerView.show()
    }
    @IBAction func gotoSelectTimesAction(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRStringPickerView(pickerMode: .componentSingle)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.dataSourceArr = self.getTimes()
        pickerView.resultModelBlock = { results in
            self.timesLabel.text = String(format: "%@", results!.value!)
            self.times = self.timesLabel.text!
        }
        pickerView.show()
    }
    @IBAction func gotoSelectRecordDate(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRDatePickerView(pickerMode: .YMD)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.resultBlock = { selectDate,selectValue in
            self.dateTF.text = selectDate?.toString(format: "yyyy.MM.dd")
        }
        pickerView.show()
    }
    
}
extension SFPAddMaternityNotesController {
    func getTemperature() -> [[String]] {
        var temperatureData: [[String]] = []
        
        var temperatureRange: [String] = []
        for temp in 35...42 {
            temperatureRange.append(String(temp))
        }
        temperatureData.append(temperatureRange)
        
        var numberList: [String] = []
        for num in 0...9 {
            numberList.append(String(num))
        }
        temperatureData.append(numberList)
        
        return temperatureData
    }
    
    func getTimes() -> [String] {
        var times: [String] = []
        for num in 0...30 {
            times.append(String(num))
        }
        return times
    }
}
